<template>
    <div v-if="store.auth.role!='学生'">
    <el-upload
            :show-file-list="false"
            :before-upload="beforeUpload"
            :on-success="onSuccess"
            :on-error="onError"
            :disabled="importDisabled"
            style="display: inline-flex;margin-right: 10px;"
            :action="'http://localhost:8080/exam/import?user='+store.auth.user">
        <el-button size="small" :disabled="importDisabled" type="success">
            <el-icon class="el-icon--left"><Upload /></el-icon>{{importDataText}}
        </el-button>
        <a href="http://localhost:8080/exam/download">下载模板</a>
    </el-upload>
    </div>
    <el-table :data="datas.slice((pageNum-1)*pageSize,pageNum*pageSize)" style="width: 100%" max-height="250" border>
        <el-table-column fixed label="序号" width="100">
            <template #default="scopes">
                {{ scopes.$index+1 }}
            </template>
        </el-table-column>
        <el-table-column fixed prop="id" label="学号" width="100" />
        <el-table-column fixed prop="name" label="姓名" width="100" />
        <el-table-column fixed prop="college" label="学院" width="200" />
        <el-table-column fixed prop="major" label="专业" width="200" />
        <el-table-column fixed prop="englishTestFour" label="英语四级" width="200" />
        <el-table-column fixed prop="computerLevel" label="计算机等级" width="200" />
        <el-table-column fixed="right" label="操作" width="200">
            <template #default="scope">
                <div>
                    <el-button
                            link
                            type="primary"
                            size="small"
                            @click.prevent="update(scope.row)"
                            v-if="store.auth.role!='学生'"
                    >
                        修改
                    </el-button>
                    <span v-if="store.auth.role=='学生'">无权限操作</span>
                </div>
            </template>
        </el-table-column>
    </el-table>
    <el-dialog title="修改信息" v-model="dialogVisible">
        <el-form :model="stus">
            <el-input placeholder="学号" v-model="stus.id" type="text" disabled/>
            <el-input placeholder="姓名" v-model="stus.name" type="text" disabled/>
            <el-input placeholder="学院" v-model="stus.college" type="text" disabled/>
            <el-input placeholder="专业" v-model="stus.major" type="text" disabled/>
            <el-input placeholder="英语四级" v-model="stus.englishTestFour" type="text" />
            <el-input placeholder="计算机等级" v-model="stus.computerLevel" type="text" />
            <el-button type="warning" @click="dialogVisible=false">取消</el-button>
            <el-button type="primary" @click="updates">确认</el-button>
        </el-form>
    </el-dialog>
    <el-pagination
            background
            layout="sizes,prev,pager,next,jumper,->,total"
            :page-size.sync="pageSize"
            :current-page.sync="pageNum"
            :page-sizes="[5,8,10,15]"
            :total="totals"
            :hide-on-single-page="true"
            @current-change="getList"
            @size-change="getList1"
    />
</template>
<script setup>
import { get,post } from '@/utils/request.js'
import {ElMessage} from 'element-plus'
import { ref,onMounted } from 'vue'
import moment from 'moment';
import { reactive } from 'vue'
import {userStore} from '@/stores/counter.js'

const store=userStore()
const datas=ref([])
const totals=ref('')
const pageNum=ref(1)
const pageSize=ref(5)
const dialogVisible=ref(false)
const dialogVisible1=ref(false)
const importDataText=ref('导入数据')
const importDisabled=ref(false)

const onError=()=>{
                importDataText.value = '导入数据';
                this.importDisabled.value = false;

                this.$message.success("导入失败！");
            }
            const onSuccess=()=> {
            	// 成功后文本修改为原来的导入数据
                importDataText.value = '导入数据';
                // 将上传组件改为允许使用
                importDisabled.value = false;
                // 调用刷新数据的方法

                // message 弹出消息
                ElMessage.success("导入成功！");
            }
const beforeUpload=()=> {
            	// 将文本修改为正在导入
                importDataText.value = '正在导入';
                // 将其上传组件暂时禁用
                importDisabled.value = true;
            }

const stus = reactive({
id:'',
name:'',
college:'',
major:'',
englishTestFour:'',
computerLevel:''
})

const findAll=()=>get('/exam/selectAll',(message)=>{
 let bbb=[]
 if (store.auth.role == '管理员') {
       datas.value=message
        totals.value = message.length
    }else if(store.auth.role=='辅导员'){
    post('/teacher/getTeaByName',{name:store.auth.user},(messages)=>{
    bbb = message.filter(item=>item.grade===messages.grade)
    datas.value=bbb.filter(item=>item.college===messages.college)
     totals.value=datas.value.length
    },(messages)=>{
    ElMessage.warning(messages)
    })
    } else {
        bbb = message.filter(item => item.name === store.auth.user)
        totals.value = bbb.length
        datas.value=bbb
    }
},(message)=>{
ElMessage.warning(message)
})

onMounted(()=>{
findAll()
})

const update=(row)=>{
stus.id=row.id
stus.name=row.name
stus.college=row.college
stus.major=row.major
stus.englishTestFour=row.englishTestFour
stus.computerLevel=row.computerLevel
dialogVisible.value=true
}

const updates=()=>{
post('/exam/update',{
id:stus.id,
englishTestFour:stus.englishTestFour,
computerLevel:stus.computerLevel,
},(message)=>{
findAll()
dialogVisible.value = false
ElMessage.success(message)
},(message)=>{
dialogVisible.value = false
ElMessage.warning(message)
})
}

const adds=(row)=>{
stu.college=row.college
dialogVisible1.value = true
}

const getList=(pageNums)=>{
pageNum.value=pageNums
}

const getList1=(pageSizes)=>{
pageSize.value=pageSizes
}




</script>
<style scoped>
</style>